﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;

namespace SMSROUTER.Business
{
    public partial class SmsShortCode
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<SmsShortCode> _instance;
        public static List<SmsShortCode> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.SmsShortCodes.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.SmsShortCodes.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region Static method

        public static SmsShortCode GetByID(int shortCodeId)
        {
            try
            {
                return GetInstance().Find(p => p.ShortCodeID == shortCodeId);
            }
            catch
            {
                return null;
            }
        }

        public static SmsShortCode GetByShortCode(string shortCode)
        {
            try
            {
                return GetInstance().Find(p => p.ShortCode == shortCode);
            }
            catch
            {
                return null;
            }
        }

        public static SmsShortCode FindItem(Predicate<SmsShortCode> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static List<SmsShortCode> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string shortcode, int? cooperateid, int? operatorid,
                                                int? mttypeid, string smppaccount, int? routerid)
        {
            var resutl = context.SmsShortCodes.AsQueryable();
            if (!string.IsNullOrWhiteSpace(shortcode))
            {
                resutl = resutl.Where(p => p.ShortCode == shortcode);
            }
            if (cooperateid != null)
            {
                resutl = resutl.Where(p => p.CooperateID == cooperateid);
            }
            if (operatorid != null)
            {
                resutl = resutl.Where(p => p.OperatorID == operatorid);
            }
            if (mttypeid != null)
            {
                resutl = resutl.Where(p => p.MTTypeID == mttypeid);
            }
            if (!string.IsNullOrWhiteSpace(smppaccount))
            {
                resutl = resutl.Where(p => p.SmppAccount == smppaccount);
            }
            if (routerid != null)
            {
                resutl = resutl.Where(p => p.RouterID == routerid);
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }

        public static IQueryable<SmsShortCodeCustom> ShortCodeCustomQuery(DBDataContext context)
        {
            return context.SmsShortCodes.Join(context.CooperateProviders,
                    outer => outer.CooperateID,
                    inner => inner.CooperateID,
                    (outer, inner) => new
                    {
                        outer.ShortCodeID,
                        outer.ShortCode,
                        outer.OperatorID,
                        outer.MTTypeID,
                        inner.CooperateName
                    }).Join(context.Operators,
                    outer => outer.OperatorID,
                    inner => inner.OperatorID,
                    (outer, inner) => new
                    {
                        outer.ShortCodeID,
                        outer.ShortCode,
                        outer.CooperateName,
                        outer.MTTypeID,
                        inner.OperatorName,
                    }).Join(context.SmsMTTypes,
                    outer => outer.MTTypeID,
                    inner => inner.MTTypeID,
                    (outer, inner) => new
                    {
                        outer.ShortCodeID,
                        outer.ShortCode,
                        outer.CooperateName,
                        outer.OperatorName,
                        inner.MTTypeCode
                    })
                    .Select(p => new SmsShortCodeCustom()
                    {
                        ShortCodeID = p.ShortCodeID,
                        ShortCode = p.ShortCode + " | " + p.CooperateName + " | " + p.OperatorName + " | " + p.MTTypeCode
                    });
        }

        public static List<SmsShortCodeCustom> GetSmsShortCodeSource()
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                return ShortCodeCustomQuery(context).ToList();
            }
        }

        public static string GetCustomNameByID(DBDataContext context, int shortCodeID)
        {
            var shortcode = ShortCodeCustomQuery(context).FirstOrDefault(p => p.ShortCodeID == shortCodeID);
            if (shortcode != null)
                return shortcode.ShortCode;
            return "";
        }

        #endregion Static method
    }

    public class SmsShortCodeCustom
    {
        public int ShortCodeID { get; set; }
        public string ShortCode { get; set; }
    }
}
